﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Dictionary
{
    public partial class SelectGoods : Form
    {
        private Int32 _contragentId;

        public Int32 ContragentId
        {
            get { return _contragentId; }
            set { _contragentId = value; }
        }

        private DataSets.waybillsData.ContragentsLinksRow _selRow;
        public DataSets.waybillsData.ContragentsLinksRow SelectedGood
        {
            get
            {
                if(_selRow==null)
                    throw new Util.DIException("Ошибка. Не выбран товар.");
                return _selRow;
            }
            set
            {
                _selRow = value;
            }
        }
        public SelectGoods(Int32 contragentId)
        {
            this.ContragentId = contragentId;
            InitializeComponent();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;
            DataSets.waybillsData.ContragentsLinksRow cr = (dgvGoods.Rows[e.RowIndex].DataBoundItem as DataRowView).Row as DataSets.waybillsData.ContragentsLinksRow;
            this.SelectedGood = cr;
            this.DialogResult = DialogResult.OK;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;
            dgvGoods.Rows[e.RowIndex].Selected = true;
        }

        private void SelectGoods_Load(object sender, EventArgs e)
        {
            InitContragents();
            DataSets.waybillsDataTableAdapters.ContragentsLinksTableAdapter adapter = new DIProject.DataSets.waybillsDataTableAdapters.ContragentsLinksTableAdapter();
            DataSets.waybillsData.ContragentsLinksDataTable cldt = adapter.GetData(this.ContragentId);
            this.dgvGoods.DataSource = cldt;
            toolStripTextBox1.TextBox.Focus();
        }
        private void InitContragents()
        {
            DataSets.SettingsTableAdapters.ContragentsTableAdapter adapter = new DIProject.DataSets.SettingsTableAdapters.ContragentsTableAdapter();
            DataSets.Settings.ContragentsDataTable cdt = adapter.GetData();
            cbContragents.ComboBox.DisplayMember = "Name";
            cbContragents.ComboBox.ValueMember = "Id";
            cbContragents.ComboBox.DataSource = cdt;
            Int32 i = this.FindItem(cbContragents.ComboBox, this.ContragentId);
            if (i != Int32.MinValue)
            {
                cbContragents.SelectedIndex = i;
            }
            
        }
        private Int32 FindItem(ComboBox cb, Int32 contragentId)
        {
            Int32 result = -1;
            foreach (object o in cb.Items)
            {
                DataRowView drv = o as DataRowView;
                DataSets.Settings.ContragentsRow cr = drv.Row as DataSets.Settings.ContragentsRow;
                result++;
                if (cr.Id == contragentId)
                {
                    return result;
                }
            }
            return Int32.MinValue;
        }

        private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                GoToPosition();
            }
        }
        private void GoToPosition()
        {
            foreach (DataGridViewRow r in dgvGoods.Rows)
            {
                DataRowView drv = r.DataBoundItem as DataRowView;
                DataSets.waybillsData.ContragentsLinksRow cr = drv.Row as DataSets.waybillsData.ContragentsLinksRow;
                string s = cr.InnerGoodName;
                string tmp = toolStripTextBox1.Text;
                Int32 i = tmp.Length;
                if (s.Length > i)
                {
                    if (string.Compare(s.Substring(0, i),tmp,true)==0)
                    {
                        dgvGoods.CurrentCell = r.Cells[0];
                        dgvGoods.Focus();
                        return;
                    }
                }
            }
        }

        private void dgvGoods_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                DataSets.waybillsData.ContragentsLinksRow cr = (dgvGoods.Rows[dgvGoods.CurrentRow.Index].DataBoundItem as DataRowView).Row as DataSets.waybillsData.ContragentsLinksRow;
                this.SelectedGood = cr;
                this.DialogResult = DialogResult.OK;
            }
        }

        private void SelectGoods_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                toolStripTextBox1.TextBox.Focus();
                toolStrip1.Focus();
            }
        }
    }
}